﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

namespace UdrinkWater_Project.Admin
{
    public partial class Sender_Product : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void detailGrid_DataSelect(object sender, EventArgs e)
        {
            Session["Order_Date"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        }

        protected void GV_OrderList_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "Total")
            {
                double Count = Convert.ToDouble(e.GetListSourceFieldValue("Product_Count"));
                double Price = Convert.ToDouble(e.GetListSourceFieldValue("Price"));
                double Distcount = Convert.ToDouble(e.GetListSourceFieldValue("Discount"));
                e.Value = (Count * Price) - Distcount;
            }
        }
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            return value;
        }
        public void Update_Status(string Date_ID)//ส่ง session พนักงาน
        {
            string sqltext = @"UPDATE [TB_Order] SET
                                      [Status]=2
                                     ,[Employee_ID]=@Employee 
                                     ,[Status_Unpaid]=1
                                     WHERE [Order_Date]=@Order_Date";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Employee", GetDataValue(Session["User"].ToString()));
            sqlcomm.Parameters.AddWithValue("@Order_Date", GetDataValue(Date_ID));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }

        protected void bt_ConfirmSender_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GV_Sender.VisibleRowCount; i++)
            {
                ASPxCheckBox Checkbox_Name = (ASPxCheckBox)GV_Sender.FindRowCellTemplateControl(i, (GridViewDataColumn)GV_Sender.Columns["colMyColumn"], "CheckBox1");

                if (Checkbox_Name.Checked == true)
                {
                    object Order_Date = GV_Sender.GetRowValues(i, "Order_Date");
                    string Date_Update = Convert.ToDateTime(Order_Date).ToString("yyyy-MM-dd HH:mm:ss.fff", new System.Globalization.CultureInfo("en-US"));
                    Update_Status(Date_Update);
                    Checkbox_Name.Checked = false;
                }
            }
            GV_Sender.DataBind();
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            UpdateExportMode();
            ASPxGridViewExporter1.WritePdfToResponse();
        }
        protected void UpdateExportMode()
        {
            GV_Sender.SettingsDetail.ExportMode = (GridViewDetailExportMode)Enum.Parse(typeof(GridViewDetailExportMode), "All");
        }
    }
}